﻿
function sendComment(newsid) {
   var req_params = "", url = "", number = 0, type = "";
   var title = "", username = "", email = "", content = "", captcha = "";
   /* Configure Parameters */
   url = "http://localhost:1813/Web/NewsService.asmx";
   title = document.getElementById("txtTitle").value;
   username = document.getElementById("txtUsername").value;
   email = document.getElementById("txtEmail").value;
   content = document.getElementById("txtContent").value;
   captcha = document.getElementById("txtCaptcha").value;
   
   if (!checkCaptcha())
       return;
   
   if (title != "Tiêu đề" && username != "Họ và tên" && content != "")
   {
       if (validateEmail(email) == false)
       {
            alert("Email không hợp lệ.");
            return;
       }
            
       req_params = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><SaveComment xmlns=\"http://tempuri.org/\">";
       req_params = req_params + "<strNewsID>" + newsid + "</strNewsID>";
       req_params = req_params + "<strTitle>" + title + "</strTitle>";
       req_params = req_params + "<strUsername>" + username + "</strUsername>";
       req_params = req_params + "<strEmail>" + email + "</strEmail>";
       req_params = req_params + "<strShareContent>" + content + "</strShareContent>";
       req_params = req_params + "</SaveComment></soap:Body></soap:Envelope>";
       /* Send XML/SOAP Request To Web Service Using Browser's Javascript DOM */
       try {
          ajax_request = new XMLHttpRequest();
          }
       catch (trymicrosoft) {
          try {
             ajax_request = new ActiveXObject("Msxml2.XMLHTTP");
             }
          catch (othermicrosoft) {
             try {
                ajax_request = new ActiveXObject("Microsoft.XMLHTTP");
                }
             catch (failed) {
                ajax_request = false;
                }
             }
          }
       ajax_request.open("POST", url, true);
       ajax_request.setRequestHeader("Content-Type", "text/xml;charset=utf-8");
       ajax_request.onreadystatechange = receiveXML_SOAPData;
       ajax_request.send(req_params);
   }
   else
   {
        var strAlert = "";
        
        if (title == "" || title == "Tiêu đề")
            strAlert += "\r\nBạn chưa nhập tiêu đề cho ý kiến của bạn.";
        if (username == "" || username == "Họ và tên")
            strAlert += "\r\nBạn chưa nhập tên của bạn.";
        if (content == "")
            strAlert += "\r\nBạn chưa nhập nội dung ý kiến.";
            
        alert(strAlert);
        return;
   }
}

function receiveXML_SOAPData() {
   if (ajax_request.readyState == 4) {
      if (ajax_request.status == 200) {
         /* Parse The Response Data */
         //result.innerText = ajax_request.responseText;
         //alert(ajax_request.responseText);
         alert("Chúng tôi đã nhận được ý kiến của bạn. Xin cảm ơn."); 
         
         document.getElementById("txtTitle").value = "Tiêu đề";
         document.getElementById("txtUsername").value = "Họ và tên";
         document.getElementById("txtEmail").value = "Địa chỉ email";
         document.getElementById("txtContent").value = "";
         document.getElementById("txtCaptcha").value = "Mã xác nhận";
         
         document.getElementById("divSharecomment").style.display = "none";
         }
      }
}

function checkCaptcha()
{
    var req_params = "", url = "", number = 0, type = "";
   var captcha = "";
   /* Configure Parameters */
   url = "http://localhost:1813/Web/NewsService.asmx";
   captcha = document.getElementById("txtCaptcha").value;
   
   req_params = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><CheckCaptcha xmlns=\"http://tempuri.org/\">";
           req_params = req_params + "<strCaptcha>" + captcha + "</strCaptcha>";
           req_params = req_params + "</CheckCaptcha></soap:Body></soap:Envelope>";
           /* Send XML/SOAP Request To Web Service Using Browser's Javascript DOM */
           try {
              ajax_request = new XMLHttpRequest();
              }
           catch (trymicrosoft) {
              try {
                 ajax_request = new ActiveXObject("Msxml2.XMLHTTP");
                 }
              catch (othermicrosoft) {
                 try {
                    ajax_request = new ActiveXObject("Microsoft.XMLHTTP");
                    }
                 catch (failed) {
                    ajax_request = false;
                    }
                 }
              }
           ajax_request.open("POST", url, true);
           ajax_request.setRequestHeader("Content-Type", "text/xml;charset=utf-8");
           ajax_request.onreadystatechange = receiveXML_CheckCaptcha;
           ajax_request.send(req_params);
           
           if (receiveXML_CheckCaptcha())
                return true;
           else
                return false;
}

function receiveXML_CheckCaptcha() {
   if (ajax_request.readyState == 4) {
      if (ajax_request.status == 200) {
         /* Parse The Response Data */
         //result.innerText = ajax_request.responseText;
         //alert(ajax_request.responseText);
         if (ajax_request.responseText.indexOf('false') != -1) {
            alert("Nhập sai mã xác nhận");
            return false;
         }
         else {
            return true;
         } 
      }
   }
}

function validateEmail(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false) {
      return false;
   }
}
function PerformClick(e, target) 
{
    if (e.which || e.keyCode)
    {
        if ((e.which == 13) || (e.keyCode == 13))
        {
            document.getElementById(target).click();
            return false;
        }
    }
    else
    {
        return true;
    } 
}